package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.User;
import service.UserService;

import java.util.List;


@Controller
@RequestMapping("/user")

public class UserController {
    @Autowired
    private UserService userService;
@RequestMapping("/login")
    public String login(User user){
         boolean flag = userService.login(user);
         if (flag){
             return "index";
         }else {
             return "login";
         }


}
    @RequestMapping("/list")
    public String findAll(Model model){
        List<User> users = userService.findAll(null,0,10);
        model.addAttribute("users",users);
        return "user/list";
    }
    @RequestMapping("/toadd")
    public String toAdd(){
        return "user/add";
    }
    @RequestMapping("/addSave")
    public String addUser(User user){
      int result =  userService.addUser(user);
      if (result>0){
          return "redirect:/user/list";
      }else{
          return "error";
      }

    }

    @RequestMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") int id){
    userService.deleteUser(id);
        return "index";
    }

    @RequestMapping("/updataSave")
    public String updataUser(){
        return "index";
    }

    @RequestMapping("/toupdata/{id}")
    public String toupdataUser(@PathVariable("id") int id){
        return "index";
    }
}
